package com.android.kotlincoroutinemvvmdemo.adapter

import android.content.Context
import com.android.kotlincoroutinemvvmdemo.Constants
import com.android.kotlincoroutinemvvmdemo.R
import com.android.kotlincoroutinemvvmdemo.bean.OneFi
import com.android.kotlincoroutinemvvmdemo.databinding.RvMainItemLayoutBinding
import com.android.kotlincoroutinemvvmdemo.event.ItemViewEvent

class OneFiAdapter(context: Context) : BaseRVAdapter<RvMainItemLayoutBinding, OneFi>() {

    private val mContext = context

    override fun getLayoutId() = R.layout.rv_main_item_layout

    override fun bindData(
        holder: RVViewHolder<RvMainItemLayoutBinding>,
        dataItem: OneFi,
        position: Int
    ) {
        dataItem.itemPosition = position
        holder.mBinding.itemViewEvent = ItemViewEvent(mContext, this)
        holder.mBinding.oneFi = dataItem
    }

    override fun getItemViewType(position: Int): Int {
        return CommonRecyclerViewPool.ITEM_TYPE
    }

    override fun enableRecyclerViewPool(): Boolean = true

    override fun localRefreshItemContent(
        holder: RVViewHolder<RvMainItemLayoutBinding>,
        position: Int,
        payloads: MutableList<Any>,
        dataItem: OneFi?
    ) {
        super.localRefreshItemContent(holder, position, payloads, dataItem)
        val payload = payloads[0]
        if (payload is String) {
            if (Constants.CHANGE_CONTENT == payload) {
                //holder.mBinding.rvMainItemRoot.setBackgroundResource(R.drawable.layout_bg_03)

                dataItem?.content = "changeContent"
                dataItem?.itemPosition = 3

                //holder.mBinding.tvMainItemContent.text = "changeContent"
            }
        }
    }

    override fun adapterUIModeNightForRecyclerView(uiModeNight: Boolean) {
        super.adapterUIModeNightForRecyclerView(uiModeNight)
        for (mBindingItem in mBindingList) {
            adapterUIModeNightForRecyclerView(mBindingItem, uiModeNight)
        }
    }

    override fun adapterUIModeNightForRecyclerView(
        mBinding: RvMainItemLayoutBinding,
        uiModeNight: Boolean
    ) {
        super.adapterUIModeNightForRecyclerView(mBinding, uiModeNight)
        if (uiModeNight) {
            mBinding.rvMainItemRoot.background = mContext.resources.getDrawable(R.drawable.layout_bg_04)
        } else {
            mBinding.rvMainItemRoot.background = mContext.resources.getDrawable(R.drawable.layout_bg_02)
        }
    }
}